package com.cyzy.mapper;

import com.cyzy.entity.Agree;
import com.cyzy.entity.ClassDO;
import com.cyzy.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Param;

public interface UserMapper {

    @Select("select * from user where username = #{username}")
    public User findByUsername(Integer username);

    @Select("select * from user where phone = #{phone}")
    public User findByPhone(String phone);

    @Select("select * from user where id = #{id}")
    public User findById(String id);

    @Select("select * from user where id_card = #{idCard}")
    public User findByIdCard(String idCard);

    @Select("select count(*) from user where username = #{username}")
    public int countByUsername(String username);

    @Insert("INSERT INTO user (password, phone, nickname, role, create_time, update_time) " +
            "VALUES (#{password}, #{phone}, #{nickname}, #{role}, NOW(), NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertUser(User user);

    //修改用户信息
    @Update("UPDATE user SET nickname = #{nickname},age = #{age},gender = #{gender}, update_time = #{updateTime} WHERE id = #{id}")
    int updateUser(User user);
    
    /**
     * 更新用户个人信息（个人中心专用）
     */
    @Update("UPDATE user SET nickname = #{nickname}, age = #{age}, gender = #{gender}, " +
            "phone = #{phone}, update_time = #{updateTime} WHERE id = #{id}")
    int updateUserProfile(User user);
    
    /**
     * 更新密码
     */
    @Update("UPDATE user SET password = #{password}, update_time = NOW() WHERE id = #{userId}")
    int updatePassword(@Param("userId") String userId, @Param("password") String password);
    
    /**
     * 更新实名认证信息
     */
    @Update("UPDATE user SET real_name = #{realName}, id_card = #{idCard}, " +
            "real_name_verified = 1, update_time = NOW() WHERE id = #{userId}")
    int updateIdentityInfo(@Param("userId") String userId, 
                          @Param("realName") String realName, 
                          @Param("idCard") String idCard);
    
    /**
     * 更新头像
     */
    @Update("UPDATE user SET avatar = #{avatarUrl}, update_time = NOW() WHERE id = #{userId}")
    int updateAvatar(@Param("userId") String userId, @Param("avatarUrl") String avatarUrl);

    @Insert("insert into agree (user_id,class_code,status) values ( #{userId}, #{classCode}, '0')")
    int joinClass(@Param("userId") String userId, @Param("classCode") String classCode);

    @Select("select * from agree where user_id = #{userId} and class_code = #{classCode}")
    Agree isJoinedClass(@Param("userId") String userId, @Param("classCode") String classCode);

    @Select("select * from class where id = #{classId}")
    ClassDO findClassById(@Param("classId") String classId);

    @Select("select * from class where class_code = #{classCode}")
    ClassDO findClassByCode(@Param("classCode") String classCode);

    @Select("select COUNT(*) from user where class_id = #{classId}")
    int countStudentsInClass(@Param("classId") String classId);

    @Update("update user set class_id = #{classId} where id = #{userId}")
    int updateUserClass(@Param("userId") String userId, @Param("classId") String classId);
}
